package top.kaoshanji.p1ch08a;

import org.apache.hc.core5.http.ContentType;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * 代码清单-p1ch08a-146页c：访问ServletContext中的属性
 * URL访问地址：/countriesServlet
 * @author kaoshanji
 * ^_^
 * create time 2025/4/7 17:41
 */
@WebServlet(urlPatterns = {"/countriesServlet"})
public class CountriesServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 设置响应对象的编码和内容格式
        resp.setContentType(ContentType.TEXT_HTML.toString());
        PrintWriter out = resp.getWriter();

        ServletContext servletContext = getServletContext();
        Map<String, String> countries =  (Map<String, String>) servletContext.getAttribute("countries");
        String mapStr = countries.toString();

        out.print("<html><head></head><body>" +
                "ServletContext在容器启动时设置的值：" + mapStr  +
                "</body></html>");

    }

}
